<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">收货人</text>
			<input class="input" type="text" v-model="addressData.cReceiver" placeholder="" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号码</text>
			<input class="input" type="number" v-model="addressData.cMobile" placeholder="+86" placeholder-class="placeholder" />
		</view>
		<view class="row b-b"  @click="openPicker()">
			<text class="tit">所在地址</text>
			<input class="input" type="text" :disabled="true" v-model="addressData.cDistrct" placeholder="" placeholder-class="placeholder" />
		
			<text class="yticon icon-you" ></text>
		</view>
		<lotus-address v-on:choseVal="choseValue" :lotusAddressData="lotusAddressData"></lotus-address>
		
		<view class="row b-b" style="height: auto;align-items: flex-start;padding: 15upx 30upx;margin-bottom: 10upx;"> 
			<text class="tit" style="font-size:23upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:rgba(153,153,153,1);">地址详情：</text>
			<textarea class="textarea" v-model="addressData.cAddress" maxlength="20" placeholder="道路、门牌号、小区、单元" placeholder-class="placeholder" />
		</view>
	<!-- 	<view class="row b-b" >
			<text class="tit">所在地址</text>
			
			<text class="addr-btn">家</text>
			<text class="addr-btn active">公司</text>
			<text class="addr-btn">学校</text>
		</view> -->
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.bDefault" color="#fa436a" @change="switchChange" style="transform:scale(0.7)" />
		</view>
		<button class="add-btn" @click="confirm">确认保存</button>
	</view>
</template>

<script>
	import lotusAddress from "@/components/Winglau14-lotusAddress/Winglau14-lotusAddress.vue";
	export default {
		components:{
			"lotus-address":lotusAddress
		},
		data() {
			return {
				addressData: {
					cReceiver: '',
					cMobile: '',
					// cDistrct: '',
					cProvince: '',
					cCity: '',
					bDefault: false,
					cArea:'',
					cRegion:''
				},
				lotusAddressData:{
					visible:false,
					provinceName:'',
					cityName:'',
					townName:'',
				},
				
				region:''
			}
		},
		onLoad(option){
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
				
				this.addressData = JSON.parse(option.data)
				// this.addressData.cDistrct = `${this.addressData.cProvince} ${this.addressData.cCity} ${this.addressData.cArea}`
				this.$set(this.addressData,"cDistrct",`${this.addressData.cProvince} ${this.addressData.cCity} ${this.addressData.cArea}`)
				this.lotusAddressData.provinceName =this.addressData.cProvince
				this.lotusAddressData.cityName = this.addressData.cCity
				this.lotusAddressData.townName = this.addressData.cArea
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			switchChange(e){
				this.addressData.bDefault = e.detail.value;
			},
			
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
					}
				})
			},
			
			//提交
			confirm(){
				let data = this.addressData;
				if(!data.cReceiver){
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if(!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.cMobile)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if(!data.cDistrct){
					this.$api.msg('请选择所在地区');
					return;
				}
				if(!data.cAddress){
					this.$api.msg('请填写详细地址');
					return;
				}
				let token = this.$SysCache.get('token')||null
				//this.$api.prePage() 获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
				this.$Request.post(this.$api.httpApi.address.saveMemberAddress+"&token="+this.$SysCache.get('token'),{"model":this.addressData}).then(res => {
					if (res.code == 200) {
							this.$api.prePage().refreshList(data, this.manageType);
							this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`);
							setTimeout(()=>{
								uni.navigateBack()
							}, 800)
							
						}
					})
				
				
				
			},
		
			openPicker() {
			            this.lotusAddressData.visible = true;
			        },
			        //回传已选的省市区的值
			        choseValue(res){
			            //res数据源包括已选省市区与省市区code
			           console.log(res.visible)
						this.addressData.cRegion = res.townCode
			            this.lotusAddressData.visible = res.visible;//visible为显示与关闭组件标识true显示false隐藏
			            //res.isChose = 1省市区已选 res.isChose = 0;未选
			            if(res.isChose){
			                this.lotusAddressData.provinceName = res.province;//省
			                this.lotusAddressData.cityName = res.city;//市
			                this.lotusAddressData.townName = res.town;//区
			                this.region = `${res.province} ${res.city} ${res.town}`; //region为已选的省市区的值
							this.addressData.cProvince = res.province
							this.addressData.cCity = res.city
							this.addressData.cArea = res.town
							this.$set(this.addressData,"cDistrct",`${res.province} ${res.city} ${res.town}`)
							
			            }
			        }
		}
	}
</script>

<style lang="scss">
	page{
		background: $page-color-base;
		// padding-top: 16upx;
	}

	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 75upx;
		background: #fff;
		
		.tit{
			flex-shrink: 0;
			width: 120upx;
			font-size:23upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:rgba(102,102,102,1);
		}
		.addr-btn{
		
			width:114upx;
			height:44upx;
			background:rgba(255,255,255,1);
			border:1upx solid rgba(210, 210, 210, 1);
			border-radius:22upx;
			font-size:23upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:rgba(102,102,102,1);
			display: flex;
			justify-content: center;
			align-items: center;
			margin-right: 30upx;
			
		}
		.active{
			background:rgba(240,64,111,1);
			color: #fff;
		}
		.input{
			flex: 1;
			font-size:23upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:rgba(153,153,153,1);
			text-align: right;
			
		}
		.textarea{
			display: inline-block;
			height: 100upx;
			font-size:23upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:rgba(153,153,153,1);
			flex: 1;
			
		}
		.icon-you{
			// flex: 1;
			// text-align: right;
			font-size:23upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			color:rgba(153,153,153,1);
		}
	}
	.default-row{
		
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		// position: fixed;
		top: 80upx;
		margin: auto;
		left:0;
		right: 0;
		bottom: 100upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width:698upx;
		height:73upx;
		background:rgba(240,64,111,1);
		border-radius:36upx;
		font-size:23upx;
		font-family:Microsoft YaHei;
		font-weight:400;
		color:rgba(255,255,255,1);
	}
</style>
